/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.AdminData;
import data.UserData;
import data.AkademikData;
import data.PrestasiData;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.AkademikDB;
import database.PrestasiDB;
import database.IdentitasDB;

/**
 *
 * @author Muhammad Dony
 */
public class ValidasAdminServlet extends HttpServlet {

    HttpSession session;
    private String action;
    AkademikDB akademikdb;
    PrestasiDB prestasidb;
    AkademikData listAkademik;

    
    
        protected void halamanValidasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        AdminData admin = (AdminData) session.getAttribute("adminMasuk");
        List listidentitas = new IdentitasDB().getIdentitassingkat();
        request.setAttribute("listidentitas", listidentitas);      
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/validasi_list.jsp");
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
         session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        String path = request.getServletPath();
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                if (path.equals("/Validasi")) {
                    try {
                        halamanValidasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
//                } else if (path.equals("/Validasi_akademik")) {
//                    try {
//                        halamanValidasiAkademik(request, response);
//                    } catch (SQLException ex) {
//                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
//                    } catch (ClassNotFoundException ex) {
//                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                } else if (path.equals("/Validasi_prestasi")) {
//                    //editPelaporan(request, response);
                }else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/login_admin.jsp");
            rd.forward(request, response);
        }

           
          
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        session = request.getSession();
        Object ud = session.getAttribute("adminMasuk");
        AdminData udata = (AdminData) ud;
        String path = request.getServletPath();
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                if (path.equals("/Validasi_listAkademik")) {
                    try {
                        halamanValidasiAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (path.equals("/Validasi_akademik")) {
                    try {
                        ValidasiAkademik(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (path.equals("/Validasi_listPrestasi")) {
                    try {
                        halamanValidasiPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (path.equals("/Validasi_prestasi")) {
                    try {
                        ValidasiPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(ValidasAdminServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

           }
        

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanValidasiAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Validasi Akademik");
        String ID  = request.getParameter("ID");
        List listakademik = new AkademikDB().getakademikById(ID);
        request.setAttribute("listakademik", listakademik);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/validasi_akademik.jsp");
        rd.forward(request, response);
    }
    
    public void ValidasiAkademik(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        //request.setAttribute("title", "Validasi Akademik");
        String ID  = request.getParameter("ID");
        action = request.getParameter("action");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listakademik = new AkademikDB().getakademikByxx(ID, no);
        AkademikData dataAkademik = (AkademikData) listakademik.get(0);
        if(action.equals("1")){
            dataAkademik.setStatus("1");
            akademikdb = new AkademikDB();
            akademikdb.updateAkademik(dataAkademik);
            halamanValidasiAkademik(request, response);
        } else if(action.equals("x")){
            dataAkademik.setStatus("x");
            akademikdb = new AkademikDB();
            akademikdb.updateAkademik(dataAkademik);
            halamanValidasiAkademik(request, response);
            
        }
       
    }
    
    public void halamanValidasiPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Validasi Prestasi");
        String ID  = request.getParameter("ID");
        List listprestasi = new PrestasiDB().getprestasiById(ID);
        request.setAttribute("listprestasi", listprestasi);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Admin/validasi_prestasi.jsp");
        rd.forward(request, response);
    }
    
    public void ValidasiPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        //request.setAttribute("title", "Validasi Prestasi");
        String ID  = request.getParameter("ID");
        action = request.getParameter("action");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listprestasi = new PrestasiDB().getprestasiByxx(ID, no);
        PrestasiData dataPrestasi = (PrestasiData) listprestasi.get(0);
        if(action.equals("1")){
            dataPrestasi.setStatus("1");
            prestasidb = new PrestasiDB();
            prestasidb.updatePrestasi(dataPrestasi);
            halamanValidasiPrestasi(request, response);
        } else if(action.equals("x")){
            dataPrestasi.setStatus("x");
            prestasidb = new PrestasiDB();
            prestasidb.updatePrestasi(dataPrestasi);
            halamanValidasiPrestasi(request, response);
            
        }
       
    }
    
}
